package com.market.plus.controller;

import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.market.plus.controller.mapper.JsonMapping;
import com.market.plus.controller.model.ContentFileInstance;
import com.market.plus.controller.model.ParticipantInstance;
import com.market.plus.controller.validator.AlbumValidator;
import com.market.plus.controller.validator.FileValidator;
import com.market.plus.controller.validator.ParticipantValidator;
import com.market.plus.core.dto.AlbumDto;
import com.market.plus.core.dto.AvatarDto;
import com.market.plus.core.dto.NewsDto;
import com.market.plus.core.dto.ParticipantDto;
import com.market.plus.core.model.MultipartFile;
import com.market.plus.core.service.NewsService;
import com.market.plus.core.service.ParticipantService;
import com.market.plus.core.util.MarketUtil;

@Controller
public class ParticipantController {

   public static final String PARTICIPANT_PAGE = "participant/private";
   public static final String REGISTER_PAGE = "participant/register";
   public static final String STATUS_PAGE = "participant/status";
   public static final String ALBUMN_PAGE = "participant/album/albums";
   public static final String ADD_ALBUMN_PAGE = "participant/album/add";
   public static final String STATUS_ALBUM_PAGE = "participant/album/status";
   public static final String STATUSHTML_PAGE = "participant/statusHTML";
   public static final String CONFIG_PAGE = "participant/configuration/config";
   public static final String UPLOAD_PICTURES_PAGE = "files/upload";
   public static final String UPLOAD_STATUS_PAGE = "files/statusHTML";

   private ParticipantService participantService;
   private ParticipantValidator participantValidator;
   private AlbumValidator albumValidator;
   private FileValidator fileValidator;
   private NewsService newsService;
   private JsonMapping jsonMapping;

   @Autowired
   public ParticipantController(
      @Qualifier("participantService") ParticipantService participantService,
      @Qualifier("newsService") NewsService newsService,
      @Qualifier("jsonMapping") JsonMapping jsonMapping,
      @Qualifier("participantValidator") ParticipantValidator participantValidator,
      @Qualifier("albumValidator") AlbumValidator albumValidator,
      @Qualifier("fileValidator") FileValidator fileValidator) {
      this.participantService = participantService;
      this.newsService = newsService;
      this.jsonMapping = jsonMapping;
      this.participantValidator = participantValidator;
      this.albumValidator = albumValidator;
      this.fileValidator = fileValidator;
      this.participantValidator.setParticipantService(participantService);
      this.albumValidator.setParticipantService(participantService);
   }

   @RequestMapping(value = "/participant/register", method = RequestMethod.GET)
   public String viewRegisterForm(ModelMap model) {
      return REGISTER_PAGE;
   }

   @RequestMapping(value = "/participant/register", method = RequestMethod.POST)
   public String registerForm(@ModelAttribute("participantIntance") ParticipantInstance participantIntance, BindingResult result, ModelMap model) throws Exception {
      ParticipantDto participantDto = participantIntance.getParticipant();
      this.participantValidator.validate(participantDto, result);
      if (!result.hasErrors()){
         this.participantService.register(participantDto, participantIntance.getFileData());
      }
      return STATUSHTML_PAGE;
   }

   @RequestMapping(value = "/home", method = RequestMethod.GET)
   public String home(ModelMap model){
      return viewPrivate(model);
   }

   @RequestMapping(value = "/viewInfo", method = RequestMethod.GET)
   public String viewPrivate(ModelMap model){
      MarketUtil.getCustomUserDetails().setCurrentViewUser(null);
      ParticipantDto participantDto = MarketUtil.getCurrentLogin();
      Long participantId  = participantDto.getId();
      List<ParticipantDto> myFriends = this.participantService.listMyFriends(participantId);
      List<ParticipantDto> suggestionFriends = this.participantService.listNotMyFriends(participantId);
      NewsDto newsDto = this.newsService.getPersonalIntroduction(participantDto.getId());
      JSONObject jsonObject = jsonMapping.convert(participantDto, newsDto, false);
      model.addAttribute("displayName", jsonObject.getString("nickname"));
      model.addAttribute("participantJSON", jsonObject);
      model.addAttribute("partId", participantDto.getId());
      AvatarDto avatarDto = participantDto.getAvatar();
      model.addAttribute("avatarGUID", (avatarDto == null)?"0":avatarDto.getContentFile().getGuid());
      model.addAttribute("friends", myFriends);
      model.addAttribute("suggestions", suggestionFriends);
      return PARTICIPANT_PAGE;
   }

   @RequestMapping(value = "/participant/{participantId}/albums", method = RequestMethod.GET)
   public String getAlbumns(@PathVariable("participantId") Long participantId, ModelMap model){
      ParticipantDto participantDto = this.participantService.findLoginUserName(true);
      List<AlbumDto> albumDtos = this.participantService.getAlbums(participantDto.getGuid());
      model.addAttribute("albums", albumDtos);
      return ALBUMN_PAGE;
   }

   @RequestMapping(value = "/participant/{participantId}/album/add", method = RequestMethod.GET)
   public String viewAddAlbum(@PathVariable("participantId") Long participantId, ModelMap model){
      model.addAttribute("participantId", participantId);
      return ADD_ALBUMN_PAGE;
   }

   @RequestMapping(value = "/participant/{participantId}/album/add", method = RequestMethod.POST)
   public String addAlbum(@PathVariable("participantId") Long participantId, @ModelAttribute("album")AlbumDto album, BindingResult result, ModelMap model) throws Exception{
      this.albumValidator.validate(album, result);
      if (!result.hasErrors()){
         this.participantService.addNewAlbum(participantId, album);
      }
      return STATUS_ALBUM_PAGE;
   }

   @RequestMapping(value = "/participant/{participantId}/file/upload", method = RequestMethod.GET)
   public String viewUploadPictures(@PathVariable("participantId") Long participantId, ModelMap model){
      model.addAttribute("participantId", participantId);
      return UPLOAD_PICTURES_PAGE;
   }

   @RequestMapping(value = "/participant/{participantId}/file/upload", method = RequestMethod.POST)
   public String uploadPictures(@PathVariable("participantId") Long participantId, @ModelAttribute("fileInstance")ContentFileInstance contentFileInstance, BindingResult result, ModelMap model) throws Exception{
      if (contentFileInstance != null && contentFileInstance.getFileData() != null){
         this.fileValidator.validate(contentFileInstance, result);
         if (!result.hasErrors()){
            MultipartFile multipartFile = new MultipartFile();
            multipartFile.setFile(contentFileInstance.getFileData());
            multipartFile.setTooltip(contentFileInstance.getTooltip());
            String uuid = this.participantService.addFileToAlbum(participantId, contentFileInstance.getAlbum(), multipartFile);
            model.addAttribute("fileUUID", uuid);
         }
      }
      return UPLOAD_STATUS_PAGE;
   }
}
